<?php

namespace App\Service\OpenShop\Feishu\Board;

use App\Enums\BaseErrorCode;
use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Exceptions\Custom\ResponseHttpException;
use App\Models\Common\ConfigModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Service\OpenShop\Feishu\BaseService;
use Illuminate\Support\Facades\Log;

class BoardService extends BaseService
{
    private int $boardType;
    private BoardParam $boardParam;

    const BOARD_TYPE_MISSION  = 1; // 任务详情
    const BOARD_TYPE_BUSINESS = 2; // 开业报告详情

    public function __construct(int $boardType, BoardParam $boardParam)
    {
        $this->boardType = $boardType;
        $this->boardParam = $boardParam;
    }

    public function getUrl(): string
    {
        switch ($this->boardType) {
            case self::BOARD_TYPE_MISSION:
                $class = MissionBoard::class;
                break;

            case self::BOARD_TYPE_BUSINESS:
                $class = BusinessBoard::class;
                break;
            default:
                return '';
        }

        $url = (new $class($this->boardParam))->valid()->url();

        Log::info("BoardService getUrl: {$url}");

        return $url;
    }
}

/**
 * Board抽象
 */
abstract class ABoard
{
   protected BoardParam $boardParam;

    public function __construct(BoardParam $boardParam)
    {
        $this->boardParam = $boardParam;
    }

    abstract protected function valid(): self;

    abstract protected function url(): string;
}

/**
 * 任务详情
 */
class MissionBoard extends ABoard
{
    public function valid(): self
    {
        if (!$this->boardParam->getMissionId()) {
            throw new ResponseHttpException(BaseErrorCode::$ERROR_OCCURRED, null, "MissionBoard missionId不能为空");
        }

        return $this;
    }

    public function url(): string
    {
        $missionId = $this->boardParam->getMissionId();

        $siteMission = OpenShopSiteMissionModel::query()->find($missionId);
        if (!$siteMission) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_ERR_NO_SITE_MISSION)
            );
        }

        // PC
        if ($this->boardParam->getAgent()->isDesktop()) {
            return BaseService::getOrderInfoWenAppUrl($siteMission->orderNo, $missionId);
        }

        // 开店小程序的类型
        if (in_array($siteMission->stepStatus, MissionTemplateEnum::OPEN_MISSION_LISTS)) {
            return BaseService::getOpenAppletUrl($missionId);
        }

        // 选址小程序的任务类型
//        if (in_array($siteMission->stepStatus, MissionTemplateEnum::SITE_LOCATION_LISTS)) {
//            return BaseService::getSiteLocationAppletUrl($missionId);
//        }

        // PC
        return BaseService::getOrderInfoWenAppUrl($siteMission->orderNo, $missionId);
    }
}

/**
 * 开业报告详情
 */
class BusinessBoard extends ABoard
{
    public function valid(): self
    {
        if (!$this->boardParam->getOrderNo()) {
            throw new ResponseHttpException(BaseErrorCode::$ERROR_OCCURRED, null, "BusinessBoard orderNo不能为空");
        }

        return $this;
    }

    public function url(): string
    {
        $orderNo = $this->boardParam->getOrderNo();
        $appid = ConfigModel::getFsConfigByPlatformNameOrgId('openShop', 0)['appId'] ?? '';

        // PC
        if ($this->boardParam->getAgent()->isDesktop()) {
            return "https://applink.feishu.cn/client/web_app/open?appId={$appid}&path=/openShop/order/info/{$orderNo}&topMenuId=45&tab=startBusiness";
        }

        // 小程序
        $path = urlencode("pages/store-detail/index?orderNo={$orderNo}&currentIdx=1&tab=startBusiness");
        return "https://applink.feishu.cn/client/mini_program/open?appId={$appid}&mode=window-semi&relaunch=true&path={$path}";
    }
}
